import { Component, OnInit } from '@angular/core';
import { Router,  ActivatedRoute } from '@angular/router';
import {RollService} from "../../../my-service/roll.service";
import {UtilService} from "../../../my-service/util.service";

@Component({
  selector: 'app-class-nav',
  templateUrl: './class-nav.component.html',
  styleUrls: ['./class-nav.component.css'],
})
export class classNavComponent implements OnInit {
  routerIndex:number = 0;//当前导航所对应的索引
  classGuid:string=null;//班级Guid
  stuGuid:string = null;
  haveUnRead:boolean =null;//是否有未读的通知
  className:string = null;//班级名字
  peoples:Number = null;//班级人数
  constructor(
  private router:Router,
  private routerInfo:ActivatedRoute,
  private rollServel:RollService,
  private  utilServe:UtilService
  ) {
  }
  /*获取班级信息*/
 getClassInfo(classGuid){
   this.rollServel.getClassList("'"+classGuid+"'").then(res=>{
     if(res.status==1){
       this.className = res.data[0].ClassName;
       this.peoples = res.data[0].Number;
     }
   })
 }
 /*获取用户是否有未读的通知*/
 getUserUnRead(classGuid,stuGuid){
   this.rollServel.getUnread(classGuid,stuGuid).then(res=>{
     if(res.status===1){
       this.haveUnRead = res.haveUnread;
       if(this.router.isActive('class/notices', true)){
         this.haveUnRead = false;
       }
     }
   })
 }
 /*获取用户Guid*/
 getUserGuid(){
   this.utilServe.getUserInfo().then(res=>{
     if(res.status==1){
       this.stuGuid =res.userGuid;
       this.getUserUnRead(this.classGuid,this.stuGuid);
     }
   })
 }
  ngOnInit() {
    this.classGuid = this.routerInfo.snapshot.params.classGuid?this.routerInfo.snapshot.params.classGuid:location.hash.split('=')[1];
    this.getUserGuid();
    this.getClassInfo(this.classGuid);
    this.matchRouter();
  }
  routerMap (index) {
    this.routerIndex = index;
    switch (index)
    {
      case 0:
        this.getUserUnRead(this.classGuid,this.stuGuid);
        this.router.navigate(['class/grade',{classGuid:this.classGuid}]);
        break;
      case 1:
        this.getUserUnRead(this.classGuid,this.stuGuid);
        this.router.navigate(['class/members',{classGuid:this.classGuid}]);
        break;
      case 2:
        this.getUserUnRead(this.classGuid,this.stuGuid);
        this.router.navigate(['class/notices',{classGuid:this.classGuid}]);
      default:
        break;
    }
  }
  goBack() {
    this.router.navigate(['home/start']);
  }
  /*匹配路由与导航条*/
  matchRouter(){
    if(this.router.isActive('class/grade', true)){
      this.routerIndex = 0;
    }else if(this.router.isActive('class/members', true)){
      this.routerIndex = 1;
    }else {
      this.routerIndex = 2;
      this.haveUnRead = false;
    }
  }
}
